package com.example.food03;

import static android.provider.MediaStore.Images.Media.getBitmap;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.food03.bool.ListAdapter;
import com.example.food03.bool.MyViewPagerAdapter;
import com.example.food03.bool.WxService;
import com.example.food03.bool.WxService5_1;
import com.example.food03.bool.WxServiceAdapter;
import com.example.food03.bool.WxServiceAdapter_2;
import com.example.food03.bool.WxServiceAdapter_3_3;
import com.example.food03.bool.WxServiceAdapter_5_1;
import com.example.food03.bool.WxServiceAdapter_5_2;
import com.example.food03.bool.shishi;
import com.example.food03.bool.shishi_;
import com.example.food03.strongweb_bool.HTMLtool;
import com.example.food03.strongweb_bool.ImgsaveUtils;
import com.example.food03.strongweb_bool.ImgsaveUtils3;
import com.example.food03.web_bool.Collection;
import com.example.food03.web_bool.Food;
import com.example.food03.web_bool.Shop;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.gson.Gson;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
    private ViewPager ViewPager,viewPager3_1,viewPager5_1;
    private BottomNavigationView bottomNavigationView;
    public static View v1,v2,v3,v4,v5,v3_1,v3_2,v5_1,v5_2;
    private List<View> mViews,mViews3_1,mViews5_1;   //存放视图
    private GridView grid1_1,grid1_2,grid3_1,grid3_2,grid5_1,grid5_2;//main_1,main_3中存放大图的一个大控件
    private List<String> list1=new ArrayList<>();  //存放读取出来的图片路径
    public static List<String> list4_1=new ArrayList<>();  //存放读取出来的图片路径
    public String list2[]= {"烤羊肉","羊肉串","烤牛肉","牛肉串","火锅","烧烤"};
    public static List<WxService> list1_1 = new ArrayList<>(16);//存放bitmap类型的list数组
    public static List<shishi_> list1_2=new ArrayList<>(16);
    public static List<shishi_> list1_3=new ArrayList<>(16);
    public static List<WxService5_1> list5_1 = new ArrayList<>(16);
    public static List<WxService5_1> list5_2 = new ArrayList<>(16);
    public ListView mList,listView4_1;
    public Button b3_1,b3_2,b4_1,b5_1,b5_2;
    public TextView t4_1;
    public ImageView imageView4_1;
    public static int q,gg,kk=0;
    public static int ii=0;
    //拉json数据下来定义的变量
    String h1_1,h1_2,h1_3,foods,collects,collects_2="";
    String food1_1[],collect1_1[],collect1_2[]=new String[100];
    public static String foodnames1_1[]=new String[100];
    public static Gson gson =new Gson();;
    public static Food food;
    public static Collection collection_food,collection_shop;
    public static String foodpics1_1[]=new String[100];
    public static int foodshuliang[]=new int[100];
    public static int shop_foodid[][]= new int[20][10];
    public static double shop_foodprice[][]=new double[100][100];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        foodshuliang[0]=4;
        //将获取到的店铺信息存放到list2里
        for(int i=0;i<MainActivity2.shopname1_1[i].length();i++){
            list2[i]=MainActivity2.shopname1_1[i];
           // Log.d("啦啦啦",MainActivity2.shopid1_1[i]+"");
        }
        //Log.d("菜单信息2",MainActivity2.shopid1_1[1]+"");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    list1_2=new ArrayList<>(16);
                    list1_3=new ArrayList<>(16);
                    //不同用户IP对应的店铺收藏信息
                    h1_2=HTMLtool.getHtml("http://192.168.43.126:8080/foodService/getAllUserCollection.do?user_id="+LoginActivity.pwd2+"&&flag=0");
                    collects=h1_2.substring(1,h1_2.length()-2);
                    collect1_1=collects.split("\\},");
                    for(int i=0;i<collect1_1.length;i++){
                    collect1_1[i]=collect1_1[i]+"}";
                    Log.d("你好2",collect1_1[i]);
                    collection_shop=gson.fromJson(collect1_1[i],Collection.class);
                   // collectpic_shop[i]=collection_shop.getPic();
                    list1_2.add(new shishi_(collection_shop.getPic(),collection_shop.getShopname()));
                    Log.d("收藏3",collection_shop.getShopname());
                    }
                    //不同用户IP对应的菜品收藏信息
                    h1_3=HTMLtool.getHtml("http://192.168.43.126:8080/foodService/getAllUserCollection.do?user_id="+LoginActivity.pwd2+"&&flag=1");
                    collects_2=h1_3.substring(1,h1_3.length()-2);
                    collect1_2=collects_2.split("\\},");
                    for(int i=0;i<collect1_2.length;i++){
                        collect1_2[i]=collect1_2[i]+"}";
                        Log.d("收藏2",collect1_2[i]);
                        collection_food=gson.fromJson(collect1_2[i],Collection.class);
                        list1_3.add(new shishi_(collection_food.getPic(),collection_food.getFoodname()));
                       // collectpic_food[i]=collection_food.getPic();
                        Log.d("收藏4",collection_food.getPic());
                    }
                    //用户登上以后
                } catch (Exception e) {
                    e.printStackTrace();
                }
                for(int j=0;j<5;j++){
                    int i =0;
                    try {
                        //店铺对应的食物信息
                    h1_1 = HTMLtool.getHtml("http://192.168.43.126:8080/foodService22/getFoodByShop.do?shop_id="+MainActivity2.shopid1_1[j]);
                    foods=h1_1.substring(1, h1_1.length()-2);
                    food1_1 = foods.split("\\},");
                    for( i = 0;i<food1_1.length+1;i++) {
                        food1_1[i] = food1_1[i] + "}";
                        food = gson.fromJson(food1_1[i], Food.class);
                        foodnames1_1[kk] = food.getFoodname();
                        foodpics1_1[kk] = food.getPic();
                        shop_foodid[j][i]=food.getFood_id();
                        shop_foodprice[j][i]=food.getPrice();
                        kk++;
                    }

                    }  catch (Exception e) {
                        e.printStackTrace();
                    }
                    foodshuliang[j]=i;

                }
            }
        }).start();
        //初始化
        init();
        //v1的监听机制
        grid1_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent;
                ImgsaveUtils3.donwloadImg(MainActivity.this,"");
                switch (position) {
                    case 0:
                        ii=position;
                       gg=0;
                        intent=new Intent(MainActivity.this,MainActivity3.class);
                        startActivity(intent);
                        break;
                    case 1:
                        ii=position;
                        gg=foodshuliang[position-1];
                        intent=new Intent(MainActivity.this,MainActivity3.class);
                        startActivity(intent);
                        break;
                    case 2:
                        ii=position;
                        gg=foodshuliang[position-1]+foodshuliang[0];
                        intent=new Intent(MainActivity.this,MainActivity3.class);
                        startActivity(intent);
                        break;
                    case 3:
                        ii=position;
                        gg=foodshuliang[position-1]+foodshuliang[0]+foodshuliang[1];
                        //Log.d("ll",gg+"");
                        intent=new Intent(MainActivity.this,MainActivity3.class);
                        startActivity(intent);
                        break;
                    case 4:
                        ii=position;
                        gg=foodshuliang[position-1]+foodshuliang[0]+foodshuliang[1]+foodshuliang[2];
                        intent=new Intent(MainActivity.this,MainActivity3.class);
                        startActivity(intent);
                        break;
                }
            }
        });
        grid1_2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Toast.makeText(MainActivity.this,"功能未开发",Toast.LENGTH_LONG).show();
                        break;
                    case 1:
                        Toast.makeText(MainActivity.this,"功能未开发",Toast.LENGTH_LONG).show();
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this,"功能未开发",Toast.LENGTH_LONG).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this,"功能未开发",Toast.LENGTH_LONG).show();
                        break;
                    case 4:
                        Toast.makeText(MainActivity.this,"功能未开发",Toast.LENGTH_LONG).show();
                        break;
                }
            }
        });
        //v3的监听机制
        b3_1.setOnClickListener(new View.OnClickListener() {
            Handler h = null;
            @Override
            public void onClick(View v) {
              /*  shishi ww=new shishi(MainActivity.this,list1_2);
                grid3_1.setAdapter(ww);*/
                viewPager3_1.setCurrentItem(0);

            }
        });
        b3_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shishi ww2=new shishi(MainActivity.this,list1_2);
                grid3_2.setAdapter(ww2);
                viewPager3_1.setCurrentItem(1);
            }
        });
        //v4的监听机制
        listView4_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        ViewPager.setCurrentItem(5);
                        //Toast.makeText(MainActivity.this,"功能未开发！",Toast.LENGTH_LONG).show();
                        break;
                    case 1:
                        Toast.makeText(MainActivity.this,"功能未开发！",Toast.LENGTH_LONG).show();
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this,"功能未开发！",Toast.LENGTH_LONG).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this,"功能未开发！",Toast.LENGTH_LONG).show();
                        break;
                    case 4:
                        alertClick();
                        q=0;
                        break;

                }
            }
        });
    }
    //初始化部件
    public void init(){
        ViewPager=(ViewPager) findViewById(R.id.views);
        bottomNavigationView=(BottomNavigationView) findViewById(R.id.nav_menu_view);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);//底部导航
        bottomNavigationView.setItemIconTintList(null);//不设置底框颜色
        //添加对应的页面
        v1= LayoutInflater.from(this).inflate(R.layout.main_1,null);
        v2=LayoutInflater.from(this).inflate(R.layout.main_2,null);
        v3=LayoutInflater.from(this).inflate(R.layout.main_3,null);
        v4=LayoutInflater.from(this).inflate(R.layout.main_4,null);
        v5=LayoutInflater.from(this).inflate(R.layout.main_5,null);
        mViews=new ArrayList<View>();//加载，添加视图
        mViews.add(v1);
        mViews.add(v2);
        mViews.add(v3);
        mViews.add(v4);
        mViews.add(v5);
        ViewPager.setAdapter(new MyViewPagerAdapter(mViews));//设置一个适配器
        ViewPager.setCurrentItem(0);
        //v1和v3初始化
        v1_and_v3_init();
        //v2初始化
        //v4初始化
        v4_init();
        //v5初始化
        v5_init();
    }
    //v1和v3的初始化
    public void v1_and_v3_init(){

        grid1_1=v1.findViewById(R.id.grid1_1);
        grid1_2=v1.findViewById(R.id.grid1_2);
        b3_1=v3.findViewById(R.id.button3);
        b3_2=v3.findViewById(R.id.button4);
        viewPager3_1=v3.findViewById(R.id.views);
        v3_1= LayoutInflater.from(this).inflate(R.layout.main_3_1,null);
        v3_2= LayoutInflater.from(this).inflate(R.layout.main_3_2,null);
        grid3_1=v3_1.findViewById(R.id.grid3);
        grid3_2=v3_2.findViewById(R.id.grid3_2);
        mViews3_1=new ArrayList<View>();//加载，添加视图
        mViews3_1.add(v3_1);
        mViews3_1.add(v3_2);
        viewPager3_1.setAdapter(new MyViewPagerAdapter(mViews3_1));//设置一个适配器
        viewPager3_1.setCurrentItem(0);
        //判断这个目录是否存在
        if (new File("/storage/emulated/0/delicious").exists()||new File("/storage/emulated/0/头像").exists()) {
            //该目录
            File files = new File("/storage/emulated/0/店铺2");
            File files4 = new File("/storage/emulated/0/头像2");
            //存放目录下图片路径的数组
            File[] file = files.listFiles();
            File[] file4 = files4.listFiles();
            for (File f : file4) {//通过for循环遍历获取到的文件数组
                list4_1.add(f.getPath());//将文件的路径添加到List1集合中
            }
            //创建一个imageView对象
            ImageView imageView =new ImageView(this);
            //v4店铺名
           /*     gson=new Gson();
            }*/
           // Log.d("菜单2","shop.getShopname()");
            if (files.exists()) {
                for (File f : file) {//通过for循环遍历获取到的文件数组
                    list1.add(f.getPath());//将文件的路径添加到List1集合中
                }

                for(int i = 0;i < list1.size();i++){
                    Bitmap bm2 = BitmapFactory.decodeFile(list1.get(i));
                    imageView.setImageBitmap(bm2);
                    //存放bitmap
                    list1_1.add(new WxService(bm2,list2[i]));
                    Log.d("你好","1");
                    //list1_2.add(new shishi_("http://192.168.43.126:8080/img/22.jpg",list2[i]));
                }
            }
        } else {
            Toast.makeText(MainActivity.this,"该目录不存在!",Toast.LENGTH_LONG).show();
        }
        //设置适配器
        WxServiceAdapter wxServiceAdapter =new WxServiceAdapter(this,list1_1);
        //shishi ww=new shishi(this,list1_2);
        //放置适配器
        grid1_1.setAdapter(wxServiceAdapter);
        grid1_2.setAdapter(wxServiceAdapter);

    }
    //v4的初始化
    public void v4_init(){
        ImgsaveUtils.donwloadImg(MainActivity.this,"");
        mList = v4.findViewById(R.id.lv);
        b4_1=v4.findViewById(R.id.button4_1);
        t4_1=v4.findViewById(R.id.textView2);
        b4_1.setOnClickListener(this);
        imageView4_1=v4.findViewById(R.id.imageView3);
        listView4_1=v4.findViewById(R.id.lv);
        //设置监听机制
        ListAdapter listAdapter=new ListAdapter();//listview的一个适配器
        SimpleAdapter spcAdapter = new SimpleAdapter(this,listAdapter.getData(),R.layout.listview1,
                new String[]{"title","img"}, new int[]{R.id.item_name,R.id.item_image});//放适配器工具
        mList.setAdapter(spcAdapter);

        if(q==1){
            t4_1.setText(LoginActivity.users2);
            b4_1.setText("id:"+LoginActivity.pwd2);
            Bitmap bm2 = BitmapFactory.decodeFile(list4_1.get(LoginActivity.pwd2-1));
            imageView4_1.setImageBitmap(bm2);
            ViewPager.setCurrentItem(3);
            Log.d("英语",LoginActivity.order_foodname[1][4]+LoginActivity.order_shopname[1][4]+LoginActivity.order_foodprice[1][4]+LoginActivity.order_foodnum[1][4]+LoginActivity.address);
        }
    }
    //v5的初始化
    public void v5_init() {
        b5_1=v5.findViewById(R.id.button5_1);
        b5_2=v5.findViewById(R.id.button5_2);
        b5_1.setOnClickListener(this);
        b5_2.setOnClickListener(this);
        viewPager5_1=v5.findViewById(R.id.views);
        v5_1= LayoutInflater.from(this).inflate(R.layout.main_5_1,null);
        v5_2= LayoutInflater.from(this).inflate(R.layout.main_5_2,null);
        grid5_1=v5_1.findViewById(R.id.grid5_1);
        grid5_2=v5_2.findViewById(R.id.grid5_2);
        mViews5_1=new ArrayList<View>();//加载，添加视图
        mViews5_1.add(v5_1);
        mViews5_1.add(v5_2);
        viewPager5_1.setAdapter(new MyViewPagerAdapter(mViews5_1));//设置一个适配器
        viewPager5_1.setCurrentItem(0);
        //存放评论数据
        if(q==1){
        int p=LoginActivity.pwd2;
        for(int i=0;i<LoginActivity.tiaoshu_sum[p];i++){
                String k=LoginActivity.order_foodname[p][i]+"   "+
                        LoginActivity.order_foodprice[p][i]+"*"+LoginActivity.order_foodnum[p][i]+"="+LoginActivity.order_foodprice[p][i]*LoginActivity.order_foodnum[p][i]+"元";
                String v=LoginActivity.address;
                String m=LoginActivity.order_shopname[p][i];
                String kk=LoginActivity.order_comment[p][i]+"("+LoginActivity.order_time[p][i]+")";
                String vv=LoginActivity.order_foodname[p][i]+"  单价："+
                        LoginActivity.order_foodprice+"元";
                //存放bitmap
                list5_1.add(new WxService5_1(k,v,m));
                list5_2.add(new WxService5_1(kk,vv,v));
                Log.d("你好1212",kk+vv);
                //list1_2.add(new shishi_("http://192.168.43.126:8080/img/22.jpg",list2[i]));

        }
            WxServiceAdapter_5_1 wxServiceAdapter =new WxServiceAdapter_5_1(this,list5_1);
            grid5_1.setAdapter(wxServiceAdapter);
            WxServiceAdapter_5_2 wxServiceAdapter2 =new WxServiceAdapter_5_2(this,list5_1);
            grid5_2.setAdapter(wxServiceAdapter2);
            }
    }
    //底部导航的监听机制
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.shouye:

                ViewPager.setCurrentItem(0);
              //  Log.d("Nav","聊天");
                break;
            case R.id.lucky:
                ViewPager.setCurrentItem(1);
               // Log.d("Nav","朋友");
                break;
            case R.id.shoucang:
                shishi ww=new shishi(MainActivity.this,list1_3);
                grid3_1.setAdapter(ww);
                ViewPager.setCurrentItem(2);
               // Log.d("Nav","通讯录");
                break;
            case R.id.wode:
                ViewPager.setCurrentItem(3);
                //Log.d("Nav","设置");
                break;

        }
        return true;
    }
    //button的监听机制
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //v4的监听机制
            case R.id.button4_1:
                if(q==0){
                    Intent intent=new Intent(MainActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
                break;
            //v5的监听机制
            case R.id.button5_1:
                Toast.makeText(this,"hh",Toast.LENGTH_LONG).show();

                 viewPager5_1.setCurrentItem(0);
                break;
            case R.id.button5_2:
                Toast.makeText(this,"hh33",Toast.LENGTH_LONG).show();
                viewPager5_1.setCurrentItem(1);
                break;
        }
    }
    //弹出对话框
    public void alertClick() {
//
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("退出确认");alertDialog.setIcon(R.drawable.ic_settings);
        alertDialog.setMessage("确定要退出登录吗？");
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "确认",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //System.exit(0);
                        Toast.makeText(MainActivity.this, "请重新登录！", Toast.LENGTH_LONG).show();
                        /*Intent intent=new Intent(MainActivity.this,LoginActivity.class);
                        startActivity(intent);*/
                        q=0;
                        b4_1.setText("点击登录");
                        t4_1.setText("未登录");
                       imageView4_1.setImageResource(R.drawable.ic____);
                        ViewPager.setCurrentItem(3);
                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "放弃",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        alertDialog.show();
        Button btnPositive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        Button btnNegative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
        layoutParams.weight = 8;
        btnPositive.setLayoutParams(layoutParams);
        btnNegative.setLayoutParams(layoutParams);

    }



}